package org.admin.controller;

import core.Data;
import core.data.DataResult;
import org.admin.entity.domain.File;
import org.admin.entity.domain.User;
import org.admin.entity.dto.FileDto;
import org.admin.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;

@RestController
@RequestMapping("/admin/file")
public class FileController {
    @Autowired
    FileService service;
    @PostMapping("/image")
    public DataResult image(@RequestParam("file") MultipartFile file,
                            @RequestParam Map<String, Object> post){
        // @RequestAttribute("currentUser") User user
        //Map<String, Object> post = new HashMap<>();
        post.put("file", file);
        return Data.create(post)
                .<FileDto>addHandle("params", data -> {
                    new FileDto().validate(new String[] {
                        "file", "width", "height", "top", "left", "right",
                        "bottom", "center", "rotate", "flip", "quality",
                    }).check(data);
                }).onSuccess(data -> {
                    data.getData().setType(File.TYPE.IMAGE.getValue());
                }).addHandle("image", data -> {
                    service.image(data, new User(){{ setId(1); }});
                }).start().getResult();
    }
    @PostMapping("/upload")
    public DataResult upload(@RequestParam("file") MultipartFile file, @RequestBody Map<String, Object> post){
        post.put("file", file);
        return Data.create(post)
                .<FileDto>addHandle("params", data -> {
                    new FileDto().validate(new String[] {
                            "*file",
                    }).check(data);
                }).addHandle("file", data -> {
                    service.image(data, new User(){{ setId(1); }});
                })
                .start().getResult();
    }
    @GetMapping("/query")
    public DataResult query(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<FileDto>addHandle("params", data -> {
                    new FileDto().validate(new String[] {
                            "type", "current", "size"
                    }).check(data);
                }).addHandle("query", data -> {
                    service.query(data, new User(){{ setId(1); }});
                }).start().getResult();
    }
    @PostMapping("/remove")
    public DataResult remove(@RequestBody Map<String, String> post){
        return Data.create(post).start().getResult();

    }

    /*
    public DataResult catch(){


    }

    public DataResult download(){

    }




    public DataResult fetch(){

    }
     */
}
